package com.king.tv.mvp.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.king.base.util.ToastUtils;
import com.king.tv.Constants;
import com.king.tv.R;
import com.king.tv.bean.LiveImageList;
import com.king.tv.http.ApiConfig;
import com.king.tv.http.BaseCallModel;
import com.king.tv.http.HttpLoader;
import com.king.tv.mvp.base.BaseFragment;
import com.king.tv.mvp.base.BasePresenter;
import com.king.tv.mvp.base.BaseView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bingoogolapple.bgabanner.BGABanner;
import cn.bingoogolapple.bgabanner.BGALocalImageSize;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.king.tv.mvp.fragment.HomeFragment.openBrowser;

/**
 * @author Jenly <a href="mailto:jenly1314@gmail.com">Jenly</a>
 * @since 2017/3/7
 */

public class LiveFragment extends BaseFragment<BaseView,BasePresenter<BaseView>> {

    @BindView(R.id.ivLeft)
    ImageView ivLeft;
    @BindView(R.id.ivRight)
    ImageView ivRight;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.banner)
    cn.bingoogolapple.bgabanner.BGABanner banner;
    private String title;
    private String slug;
    private boolean isTabLive;

    public static LiveFragment newInstance(String title,String slug, boolean isTabLive) {
        Bundle args = new Bundle();
        LiveFragment fragment = new LiveFragment();
        fragment.title = title;
        fragment.slug = slug;
        fragment.isTabLive = isTabLive;
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getRootViewId() {
        return R.layout.fragment_live;
    }

    @Override
    public void initUI() {
        tvTitle.setText(title);
        if(isTabLive){
            ivLeft.setImageResource(R.drawable.ic_top_search);
            ivLeft.setVisibility(View.GONE);
            ivRight.setVisibility(View.VISIBLE);
        }else{
            ivLeft.setVisibility(View.GONE);
            ivLeft.setImageResource(R.drawable.btn_back_selector);
            ivRight.setVisibility(View.INVISIBLE);
        }
        initTab();
        replaceChildFragment(R.id.fragment,LiveListFragment.newInstance(slug));
    }

    private void initTab() {
        HttpLoader.getInstance().getLiveImage().subscribe(new Observer<BaseCallModel<Object>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseCallModel<Object> value) {
                Gson gson = new Gson();
                String json = gson.toJson(value);
                BaseCallModel baseCallModel = gson.fromJson(json,BaseCallModel.class);
                if(baseCallModel!=null){
                    String json1 = gson.toJson(baseCallModel.getData());
                    final List<LiveImageList> liveImageLists = gson.fromJson(json1,new TypeToken<List<LiveImageList>>(){}.getType());
                    BGALocalImageSize localImageSize = new BGALocalImageSize(720, 230, 480, 200);
                    banner.setAdapter(new BGABanner.Adapter<LinearLayout, String>() {
                        @Override
                        public void fillBannerItem(BGABanner banner, LinearLayout itemView, String model, int position) {
                            ImageView simpleDraweeView = itemView.findViewById(R.id.iv_item_fresco);
                            Glide.with(getContext()).load(model).apply(RequestOptions.bitmapTransform(new RoundedCorners(10))).diskCacheStrategy(DiskCacheStrategy.RESOURCE).into(simpleDraweeView);
                        }
                    });
                    //设置数据源
                    List<String> list = new ArrayList<>();
                    for(LiveImageList e:liveImageLists){
                        list.add(ApiConfig.BASE_URL+e.getImg());
                    }
                    banner.setData(R.layout.item_fresco, list, null);
                    banner.setDelegate(new BGABanner.Delegate<LinearLayout, String>() {
                        @Override
                        public void onBannerItemClick(BGABanner banner, LinearLayout itemView, String model, int position) {
                            Log.i("tag","==============liveimage"+ liveImageLists.get(banner.getCurrentItem()).getUrl());
                            Log.i("tag","==============liveimage"+position);
                            if(liveImageLists.get(banner.getCurrentItem()).getUrl().startsWith("http")){
                                openBrowser(getActivity(),liveImageLists.get(banner.getCurrentItem()).getUrl());}
                        }
                    });
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void initData() {

    }

    private void clickLeft(){
        if(isTabLive){
            startActivity(getFragmentIntent(Constants.SEARCH_FRAGMENT));
        }else{
            finish();
        }
    }

    @OnClick({R.id.ivLeft,R.id.ivRight})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.ivLeft:
                clickLeft();
                break;
            case R.id.ivRight:
                startLogin();
                break;
        }
    }


    @Override
    public BasePresenter<BaseView> createPresenter() {
        return new BasePresenter<BaseView>(getApp());
    }
}
